import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

const updateListeners: any = {}

// Custom APIs for renderer
const api = {
	minimizeWindow: () => ipcRenderer.invoke('window-minimize'),
	maximizeWindow: () => ipcRenderer.invoke('window-maximize'),
	closeWindow: () => ipcRenderer.invoke('window-close'),


  // 版本升级
  checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
  downloadUpdate: () => ipcRenderer.invoke('download-update'),
  quitAndInstall: () => ipcRenderer.invoke('quit-and-install'),
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),

  // 系统配置
  getConfig: () => ipcRenderer.invoke('get-config'),

  // cookie
  setCookie: (cookie) => ipcRenderer.invoke('set-cookie', cookie),
  getCookies: (url) => ipcRenderer.invoke('get-cookies', url),
  deleteCookie: (url, name) => ipcRenderer.invoke('delete-cookie', url, name),

  onCookieChanged: (callback) => {
    ipcRenderer.on('cookie-changed', (_event, cookie) => {
      callback(cookie)
    })
  },


  // 打开新窗口
  openNewWindow: (url, options, inject) => ipcRenderer.invoke('open-new-window', url, options, inject),

  // 关闭当前窗口
  closeNewWindow: (windowId) => ipcRenderer.invoke('close-new-window', windowId),

  // 根据ID查询
  findNewWindow: (windowId) => ipcRenderer.invoke('find-new-window', windowId),


  // 打开webview
  openWebview: (url, options, inject) => ipcRenderer.invoke('open-web-view', url, options, inject),

  // 关闭当前窗口
  closeWebview: (viewId) => ipcRenderer.invoke('close-web-view', viewId),

  // 执行脚本
  executeJavaScript: (viewId, script) => ipcRenderer.invoke('execute-javascrpt', viewId, script),



  // 更新事件监听
  onUpdateAvailable: (callback) => {
    updateListeners.updateAvailable = callback
    ipcRenderer.on('update-available', callback)
  },

  onUpdateNotAvailable: (callback) => {
    updateListeners.updateNotAvailable = callback
    ipcRenderer.on('update-not-available', callback)
  },

  onUpdateProgress: (callback) => {
    updateListeners.updateProgress = callback
    ipcRenderer.on('update-progress', callback)
  },

  onUpdateDownloaded: (callback) => {
    updateListeners.updateDownloaded = callback
    ipcRenderer.on('update-downloaded', callback)
  },

  onUpdateError: (callback) => {
    updateListeners.updateError = callback
    ipcRenderer.on('update-error', callback)
  },

  onUpdateStatus: (callback) => {
    updateListeners.updateStatus = callback
    ipcRenderer.on('update-status', callback)
  },

  // 移除所有监听器
  removeAllUpdateListeners: () => {
    Object.values(updateListeners).forEach((callback:any) => {
      if (callback) {
        ipcRenderer.removeAllListeners(callback)
      }
    })
  }
}


// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('electronApi', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.electronApi = api
}
